﻿using System;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Hn.Database.DAL;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using MSocketCore.Cmd;
using MSocketCore.Hub;
using MSocketCore.Model;
using SuperSocket;
using SuperSocket.Command;

namespace MSocketCore.MSocket
{
    [MyCmdFilter]
    [Command(Key = "02")]
    public class AFN02 : IAsyncCommand<MySession,MyPackage>
    {
        private readonly MessageClient _client;
        private readonly ILogger<AFN02> _logger;
        private readonly IHostEnvironment _env;
        private readonly IOptions<MConfig> _config;

        public AFN02(MessageClient client, ILogger<AFN02> logger, IHostEnvironment env,IOptions<MConfig> config)
        {
            _client = client;
            _logger = logger;
            _env = env;
            _config = config;
        }

        public ValueTask ExecuteAsync(MySession session, MyPackage info)
        {
            //var path = _config.Value.BinPath;
            //using (var fs = File.Open(path, FileMode.Open))
            //{
            //    var buff = new byte[fs.Length];
            //    fs.Read(buff, 0, buff.Length);
            //    _logger.LogInformation(buff.ToList().Count.ToString());
            //}

            _logger.LogInformation($"App Recevice:{info.Sn}[Detect]\n{info.Message}");
            switch (info._Fn)
            {
                case 1:
                    PacketHelper.Confirm(session, info);

                    //时间校准
                    Thread.Sleep(2000);

                    PacketHelper.ConfirmTime(session, info);

                    //缓存报文
                    Thread.Sleep(2000);
                    var cachecmd =CmdCacheDAL.GetUnSend(info.Sn);
                    foreach (var cmd in cachecmd)
                    {
                        session.SendAsync(StringToByte(cmd.cmd));
                        cmd.updateTime =DateTime.Now;
                        cmd.state = 1;
                        CmdCacheDAL.Update(cmd);
                        _logger.LogInformation($"Send Cached Cmd:{info.Sn}[Detect]\n{cmd.cmd}");
                    }
                    break;

                case 3:
                    Thread.Sleep(1000);
                    PacketHelper.Confirm(session, info);
                    //MsgHelper.ConfirmTime(session, requestInfo);
                    //if (requestInfo.Content.Length > 0)
                    //{
                    //    LogHelper.Info("心跳时间：" + requestInfo.Sn + " " + CmdHelper.GetBCD_A1(requestInfo.Content));
                    //}
                    //else
                    //{
                    //    LogHelper.Info("心跳时间：" + requestInfo.Sn + " " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                    //}
                    break;
            }

            return new ValueTask();
        }

        private byte[] StringToByte(string hexString)
        {
            hexString = hexString.Replace(" ", "");//去掉空格
            if ((hexString.Length % 2) != 0)
                hexString += " ";
            byte[] returnBytes = new byte[hexString.Length / 2];
            for (int i = 0; i < returnBytes.Length; i++)
                returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
            return returnBytes;
        }
    }
}